package dshumsky.akka;

import java.util.concurrent.TimeUnit;

import scala.concurrent.Await;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.Patterns;
import akka.util.Timeout;


public class MapReduceApplication {
    public static void main(String[] args)
        throws Exception {
        Timeout timeout = new Timeout(60, TimeUnit.SECONDS);
        ActorSystem _system = ActorSystem.create("MapReduceApp");
        ActorRef master = _system.actorOf(new Props(MasterActor.class), "master");
        long start = System.currentTimeMillis();

        for (int i = 1; i <= 1000; i++) {
            master.tell("The quick brown fox tried to jump over the lazy dog and fell on the dog " + randX(), master);
            master.tell("Dog is man's best friend " + randX(), master);
            master.tell("Dog and Fox belong to the same family " + randX(), master);
        }

        // Thread.sleep(5000);
        scala.concurrent.Future<Object> future = Patterns.ask(master, new Result(), timeout);
        String result = (String) Await.result(future, timeout.duration());
        long end = System.currentTimeMillis();
        System.out.println("time=" + (end - start) + "ms , result=" + result);
        _system.shutdown();
    }

    private static int randX() {
        return (int) (Math.random() * 10);
    }
}
